<template>
  <div class="search-box">
    <input
      type="text"
      :placeholder="placeholder"
      @focus="isFoucs = true"
      @blur="onSearchInputBlur()"
      @keyup="onSearch"
    />
    <transition name="fade">
      <ul class="sug-list" v-show="sugList.length">
        <transition-group name="list-complete">
          <template v-for="(item, index) in sugList">
            <li :key="item" :class="index + 1 === activeSugItem ? 'act':''" @click="onClickSug(item)">{{ item }}</li>
          </template>
        </transition-group>
      </ul>
    </transition>
  </div>
</template>
<script>
  import { $jsonp, throttle } from '../../utils/ajax'

  export default {
    props: {
      placeholder: {
        type: String,
        default: ' '
      }
    },
    data() {
      return {
        isFoucs: false,
        sugList: [],

        activeSugItem: 0
      }
    },
    watch: {
      'isFoucs': {
        handler() {
          this.$emit('getIsFoucs', this.isFoucs)
        },
        immediate: true
      }
    },
    methods: {
      // 节流
      throttleSearch(value) {
        $jsonp({
          url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
          data: {
            wd: value
          },
          callback: 'cb',
          time: 2000,
          success: (res) => {
            this.sugList = res.s
          }
        })
      },
      // 搜索
      onSearch(e) {
        const { value } = e.target
        if (e.key === 'Enter' && !this.activeSugItem) {
          if (value) {
            window.open('https://www.baidu.com/s?wd=' + value)
          }
        }
        if (e.key === 'Enter' && !value) {
          window.open('https://www.baidu.com/s?wd=' + this.placeholder)
        }
        if (!value) return
        if (this.sugList.length) {
          if (e.key === 'ArrowDown') {
            this.activeSugItem < this.sugList.length && this.activeSugItem++
          } else if (e.key === 'ArrowUp') {
            this.activeSugItem > 0 && this.activeSugItem--
          }
          if (e.key === 'Enter') {
            window.open('https://www.baidu.com/s?wd=' + this.sugList[this.activeSugItem - 1])
          }
        }
        throttle(this.throttleSearch(value))
      },
      // 点击搜索建议
      onClickSug(item) {
        this.sugList = []
        window.open('https://www.baidu.com/s?wd=' + item)
      },
      onSearchInputBlur() {
        this.isFoucs = false
        setTimeout(() => {
          this.sugList = []
        }, 200)
      },
    }
  }
</script>
<style lang="less">
  // 搜索
  .search-box {
    width: 80%;
    max-width: 760px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    margin-top: 100px;
    transition: margin-top .36s;
    input {
      width: 80%;
      height: 46px;
      line-height: 46px;
      padding: 0 16px;
      border-radius: 28px;
      color: #000;
      transition: all .42s;
      transition-delay: calc(var(--dur) * 0.25);
      box-shadow: rgba(0,0,0,.2) 0 0 10px;
      background-color: rgba(255,255,255,0.2);
      &:focus {
        width: 100%;
        background-color: rgba(255,255,255,0.8);
      }
    }
    .sug-list {
      width: 90%;
      padding: 10px 0;
      position: absolute;
      top: 47px;
      left: 0;
      background-color: rgba(255, 255, 255, 0.9);
      margin-left: 5%;
      border-radius: 6px;
      z-index: 2;
      transition: all 0.26s;
      box-sizing: border-box;
      li {
        padding: 8px 15px;
        transition: all 0.52s;
        &:hover,
        &.act {
          cursor: pointer;
          color: #4e8cea;
          background: #ffffff;
          padding-left: 20px;
        }
      }
    }
  }
@media screen and (max-width: 520px) {
  .search-box {
    margin-top: 140px;
    input {
      width: 100%;
      border-radius: 8px;
    }
    .sug-list {
      width: 100%;
      margin-left: 0;
    }
  }
}
</style>
